// compotents/frame/index.js
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    frames: {
      type: Array,
      value: []
    },
    autoPlay: {
      type: Boolean,
      value: false
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    // isPlay: false,
    current: 0,
    speed: 40,
  },

  lifetimes: {
    // attached() {
    //   this.setData({
    //     isPlay: this.properties.autoPlay // 将父组件传来的属性赋值给内部数据
    //   });
    // },
    ready() {
      if (this.properties.autoPlay)
        this.play();
    },
    detached() {
      this.stop();
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {

    play() {
      console.log("play");
      clearInterval(this.timer);
      this.timer = setInterval(() => {
        let current = this.data.current + 1;
        if (current >= this.properties.frames.length) this.stop();
        // if (current >= this.properties.frames.length) current = 0;
        else this.setData({ current });
      }, this.data.speed)
    },
    stop() {
      clearInterval(this.timer);
      this.data.current = 0;
      this.setData({ current: 0 });
      this.triggerEvent('ended');
      console.log("stop");
    }
  }
})